package com.sumainfor.pc.market.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.sumainfor.common.exception.TCException;
import com.sumainfor.common.util.CustomMap;
import com.sumainfor.common.util.JsonResult;
import com.sumainfor.common.util.UUIDUtils;
import com.sumainfor.common.utlis.ParamsCommon;
import com.sumainfor.common.wechat.MapUtil;
import com.sumainfor.pc.market.dao.RunProviderMapper;
import com.sumainfor.pc.market.feign.Zfeign.ZzuulFeign;
import com.sumainfor.pc.market.service.RunProviderService;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.collections.MapUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @Author Eddy·Chen
 * @Email 835033913@qq.com
 * @Create 2019-03-07 10:46
 * 运营部服务提供者
 */
@Service
public class RunProviderServiceImpl implements RunProviderService {

    @Autowired
    private RunProviderMapper runProviderMapper;

    @Autowired
    private ZzuulFeign zuulFeign;

    /**
     * 通过房源详表编号查询 运营部所需要的数据
     *
     * @param houInfoId
     * @return
     */
    @Override
    public JsonResult getHousingInfoById(String houInfoId) {
        Map<String, Object> data = runProviderMapper.selectHousingByInfoId(houInfoId);
        if (MapUtils.isEmpty(data)) {
            return JsonResult.error("房源不存在");
        }
        //获得归属中心民称
        JsonResult<List<Map<String, Object>>> dataResult = zuulFeign.getDeptNameByDeptId(Arrays.asList(MapUtils.getString(data, "MiId")));
        if (dataResult.isSuccess() == false) {
            return JsonResult.error("查询失败");
        }
        if (CollectionUtils.isEmpty(dataResult.getResult())) {
            return JsonResult.error("查询失败");
        }
        String name = (String) dataResult.getResult().get(0).get("name");
        data.put("DeptName", name);
        data.remove("MiId");
        return JsonResult.success(data);
    }

    /**
     * 获取小区列表 通过服务中心编号
     *
     * @param centerId 服务中心标号
     * @return
     */
    @Override
    public Page<Map<String, Object>> getProjectListByCenterId(Page page, String centerId) {
        return runProviderMapper.selectProjectListByCenterId(page, centerId);
    }

    /**
     * 获得房源数据-通过小区编号
     *
     * @param page
     * @param params 条件
     * @return
     */
    @Override
    public Page<Map<String, Object>> getHousingListByProjectId(Page page, Map<String, Object> params) {
        return runProviderMapper.selectHousingByProjectId(page, params);
    }


    /**
     * 获得房源信息 业主提前收房用
     *
     * @param houId
     * @return
     */
    @Override
    public Map<String, Object> getOmHousingInfoById(String houId) {
        return runProviderMapper.getOmHousingInfoById(houId);
    }


    /**
     * 修改业主联系方式
     *
     * @param params
     * @return
     */
    @Override
    public JsonResult updateOwCallInfo(Map<String, Object> params) {
        int row = runProviderMapper.updateOwCallInfo(ParamsCommon.commonUpdateParms(CustomMap.create()
                .put("OwId", params.get("OwId"))
                .put("OwName", params.get("NewContact"))
                .put("OwPhone", params.get("NewPhone")))
        );
        return row <= 0 ? JsonResult.actionFailure() : JsonResult.actionSuccess();
    }

    /**
     * 修改业主人员信息
     *
     * @param list
     * @return
     */
    @Override
    public JsonResult updateOwPeopleInfo(List<Map<String, Object>> list) {
        for (Map<String, Object> map : list) {
            Integer type = MapUtils.getInteger(map, "OperType");
            if (type.equals(1)) {
                //新增
                String id = UUIDUtils.generateShortUuid();
                int row = runProviderMapper.insertOwnerPeople(ParamsCommon.commonInsertParms(CustomMap.create()
                        .put("HouOwId", id)
                        .put("HouId", map.get("HouId"))
                        .put("HouOwName", map.get("HouOwName"))
                        .put("IdnoType", 1)
                        .put("IdnoNumber", map.get("IdnoNumber"))
                        .put("IdnoImgs", map.get("IdnoImgs")), getClass())
                );
            } else if (type.equals(2)) {
                //删除
                String id = MapUtils.getString(map, "HouOwId");
                int row = runProviderMapper.deletePeopleById(id);
            } else {
                throw new TCException("类型错误");
            }
        }
        return JsonResult.actionSuccess();
    }


    /**
     * 修改业主银行收款信息
     *
     * @param params
     * @return
     */
    @Override
    public JsonResult updateOwPactInfo(Map<String, Object> params) {
        String pactId = runProviderMapper.getCurrentOwnerPactByOwnerId(MapUtils.getString(params, "OwId"));
        int row = runProviderMapper.updateOwPactInfo(ParamsCommon.commonUpdateParms(CustomMap.create()
                .put("OwId", params.get("OwId"))
                .put("OwDeId", pactId)
                .put("Payee", params.get("NewPayee"))
                .put("PayAccount", params.get("NewPayAccount"))
                .put("PayBank", params.get("NewPayBank")))
        );
        return row <= 0 ? JsonResult.actionFailure() : JsonResult.actionSuccess();
    }


    /**
     * 获取业主资料信息通过业主的手机号或者业主编号
     *
     * @param params
     * @return
     */
    @Override
    public Map<String, Object> getOwnerInfoByParams(Map<String, Object> params) {
        return runProviderMapper.getOwnerInfoByParams(params);
    }

    /**
     * 通过业主的手机号码 查询业主信息
     *
     * @param phone
     * @return
     */
    @Override
    public List<Map<String, Object>> getOwnerInfoListByPhone(String phone) {
        return runProviderMapper.getOwnerInfoListByPhone(phone);
    }


    /**
     * 通过业主编号查询这个业主的房源所在地理位置
     *
     * @param ownerId
     * @return
     */
    @Override
    public Map<String, Object> getAddressInfo(String ownerId) {
        return runProviderMapper.getAddressInfo(ownerId);
    }

    /**
     * 获取业主履行中的合同信息
     *
     * @param ownerId
     * @return
     */
    @Override
    public List<Map<String, Object>> selectOwnerPactList(List<String> ownerId) {
        return runProviderMapper.selectOwnerPactList(ownerId).stream().map(item -> {
            String miId = MapUtils.getString(item, "MiId");
            //获取服务中心名称
            item.put("CenterName", "-");
            if (StringUtils.isNotBlank(miId)) {
                Map<String, Object> deptInfo = zuulFeign.getDeptInfoById(miId);
                if (MapUtils.isNotEmpty(deptInfo)) {
                    item.put("CenterName", deptInfo.get("name"));
                }
            }
            return item;
        }).collect(Collectors.toList());
    }

    /**
     * 查询业主人员信息数据
     *
     * @param houId
     * @return
     */
    @Override
    public List<Map<String, Object>> getOwnerPeopleData(String houId) {
        return runProviderMapper.getOwnerPeopleData(houId);
    }


    /**
     * 根据业主信息 去查询业主原来的收款信息
     *
     * @param owId
     * @return
     */
    @Override
    public Map<String, Object> getOwnerOldBlankInfo(String owId) {
        return runProviderMapper.selectOwnerBlankInfo(owId);
    }


    ///////////////////////////////////////////////////////////////////////////
    // 退租账单
    ///////////////////////////////////////////////////////////////////////////


    @Override
    public IPage<Map<String, Object>> selectSurrenderList(Page page, Map<String, Object> params) {
        IPage<Map<String, Object>> surrender = runProviderMapper.selectSurrender(page, params);
        surrender.setRecords(surrender.getRecords().stream().map(item -> {
            String miId = MapUtils.getString(item, "MiId");
            //获取服务中心名称
            item.put("CenterName", "-");
            if (StringUtils.isNotBlank(miId)) {
                Map<String, Object> deptInfo = zuulFeign.getDeptInfoById(miId);
                if (MapUtils.isNotEmpty(deptInfo)) {
                    item.put("CenterName", deptInfo.get("name"));
                }
            }
            return item;
        }).collect(Collectors.toList()));
        return surrender;
    }


    /**
     * 查询合同到期时间
     *
     * @param pactId
     * @return
     */
    @Override
    public String getPactPastTime(String pactId) {
        return runProviderMapper.getPactPastTime(pactId);
    }

    /**
     * 查询活动名称
     *
     * @param actId
     * @return
     */
    @Override
    public String getActivateNameById(String actId) {
        return runProviderMapper.getActivateNameById(actId);
    }

    ///////////////////////////////////////////////////////////////////////////
    // 地区收费标准
    ///////////////////////////////////////////////////////////////////////////

    /**
     * 根据服务中心的编号查询小区数据列表
     *
     * @param centerId
     * @param page
     * @return
     */
    @Override
    public IPage<Map<String, Object>> selectProjectByCenterId(Page page, String centerId) {
        return runProviderMapper.selectProjectByCenterId(page, centerId);
    }

    /**
     * 根据项目的编号查询房源的数据列表
     *
     * @param page
     * @param params: 参数：
     *                projectId - 小区编号
     *                centerId - 服务中心编号
     *                where - 条件
     *                -par1 在租中
     *                -par2 待租中
     *                -par3 装修中
     *                -par4 整租
     *                -par5 合租
     * @return
     */
    @Override
    public IPage<Map<String, Object>> selectHousingListView(Page page, Map<String, Object> params) {
        return runProviderMapper.selectHousingListView(page, params);
    }


}
